package com.ytjj.qmyx.admin.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.gexin.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.dao.BankerInfoDao;
import com.ytjj.qmyx.admin.dao.BankerMessageDao;
import com.ytjj.qmyx.admin.mapper.AdminMapper;
import com.ytjj.qmyx.admin.model.request.BankerInfoRequest;
import com.ytjj.qmyx.admin.model.response.BankerInfoResponse;
import com.ytjj.qmyx.admin.service.BankerInfoService;
import com.ytjj.qmyx.admin.service.MemberService;
import com.ytjj.qmyx.mall.model.BankerInfo;
import com.ytjj.qmyx.mall.model.BankerMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 * 
 * @Version: 1.0
 */
@Service
public class BankerInfoServiceImpl implements BankerInfoService {

    @Autowired
    private BankerInfoDao bankerInfoDao;

    @Autowired
    private BankerMessageDao bankerMessageDao;

    @Autowired
    private MemberService memberService;

    @Autowired
    private AdminMapper adminMapper;

    @Resource
    private SupplyApiConfig supplyApiConfig;

    @Override
    public CommonPage<BankerInfoResponse> selectList(BankerInfoRequest bankerInfoRequest) {
        PageHelper.startPage(bankerInfoRequest.getPageNum(), bankerInfoRequest.getPageSize());
//        String curUserEmail = UserUtil.getPrincipal();
//        int adminId =adminMapper.adminId(curUserEmail);
//        bankerInfoRequest.setAdminId(adminId);
        List<BankerInfoResponse> pageList = bankerInfoDao.selectList(bankerInfoRequest);
        if(!CollectionUtils.isEmpty(pageList)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            for(BankerInfoResponse info : pageList){
                if (null == info.getForver() || 1 == info.getForver()) {
                    info.setBusinessLimit("长期");
                }else {
                    String startTime = "";
                    String endTime = "";
                    if (null != info.getBusinessLimitStart()) {
                        startTime = sdf.format(info.getBusinessLimitStart());
                    }
                    if (null != info.getBusinessLimitEnd()) {
                        endTime = sdf.format(info.getBusinessLimitEnd());
                    }
                    if (StringUtils.isEmpty(startTime) && StringUtils.isEmpty(endTime)) {
                        info.setBusinessLimit("");
                    }else {
                        info.setBusinessLimit(startTime + "至" + endTime);
                    }
                }
                // 商标注册证
                String cate = info.getRegisterCertificate();
                List<String> cateList = new ArrayList<>();
                if(cate != null) {
                    String[] array = cate.split(",");
                    for(int i = 0; i<array.length; i++) {
                        cateList.add(array[i]);
                    }
                }
                info.setCateLists(cateList);
                // 品牌授权书
                String licen = info.getBrandLicensing();
                List<String> licenList = new ArrayList<>();
                if(licen != null) {
                    String[] array = licen.split(",");
                    for(int i = 0; i<array.length; i++) {
                        licenList.add(array[i]);
                    }
                }
                info.setLicenLists(licenList);
            }
        }
        return CommonPage.restPage(pageList);
    }

    @Override
    public String newSelectList(BankerInfoRequest bankerInfoRequest) {
        String json = JSON.toJSONString(bankerInfoRequest);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/banker/bodyList", json, supplyApiConfig.getHeadsMap());
        return resultJson;
    }

    @Override
    public BankerInfo selectIdentity(Integer id) {
        return bankerInfoDao.selectIdentity(id);
    }

    @Override
    public String newSelectIdentity(Integer id) {
        Map map = new HashMap();
        map.put("id",String.valueOf(id));
        String resultJson = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/banker/selectIdentity", map, supplyApiConfig.getHeadsMap());
        return resultJson;
    }

    @Override
    public Integer verifySuccess(Integer id) {
        Integer count = bankerInfoDao.verifySuccess(id, memberService.getAdmin().getName());
        if (count > 0) {
            BankerInfo bankerInfo = bankerInfoDao.getBankerInfo(id);
            BankerMessage message = new BankerMessage();
            message.setBankerId(bankerInfo.getBankerId());
            message.setBankerName(bankerInfo.getBankerName());
            message.setRemarks("您上传的商家信息："+bankerInfo.getBankerName()+" 已通过审核！");
            message.setStatus(0);
            message.setType(5);
            message.setCreateTime(new Date());
            bankerMessageDao.insertMessage(message);
        }
        return count;
    }

    @Override
    public String newVerifySuccess(BankerInfoRequest bankerInfoRequest) {
        String json = JSON.toJSONString(bankerInfoRequest);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/banker/verifySuccess", json, supplyApiConfig.getHeadsMap());
        return resultJson;
    }

    @Override
    public Integer verifyFail(Integer id, String rejectReason) {
        Integer count = bankerInfoDao.verifyFail(id, rejectReason, memberService.getAdmin().getName());
        if (count > 0) {
            BankerInfo bankerInfo = bankerInfoDao.getBankerInfo(id);
            BankerMessage message = new BankerMessage();
            message.setBankerId(bankerInfo.getBankerId());
            message.setBankerName(bankerInfo.getBankerName());
            message.setRemarks("您上传的商家信息："+bankerInfo.getBankerName()+" 未通过审核,原因:("+rejectReason+")");
            message.setStatus(0);
            message.setType(6);
            message.setCreateTime(new Date());
            bankerMessageDao.insertMessage(message);
        }
        return count;
    }

    @Override
    public String newVerifyFail(BankerInfoRequest bankerInfoRequest) {
        String json = JSON.toJSONString(bankerInfoRequest);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/banker/verifyFailBankerInfo", json, supplyApiConfig.getHeadsMap());
        return resultJson;
    }

    @Override
    public BankerInfo getBankerInfoStatus(Integer bankerId) {
        return bankerInfoDao.getBankerInfoStatus(bankerId);
    }
}
